package com.demo.test;

import java.util.Arrays;
import java.util.Scanner;

public class JinE {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        if (line.isEmpty()){
            System.out.println(-1);
        }
        String[] amountStr =  line.split(",");
        if (amountStr.length < 3) {
            System.out.println(-1);
            return;
        }
        int[] amount = new int[amountStr.length];
        for (int i = 0; i < amountStr.length; i++) {
            amount[i] = Integer.parseInt(amountStr[i]);
        }
        Arrays.sort(amount);

        int money = sc.nextInt();

        // 不满足条件
        if (money < amount[0] + amount[1] + amount[2]) {
            System.out.println(-1);
            return;
        }
        int max = 0;
        // 满足条件
        for (int i = 0; i < amount.length - 2; i++) {
            for (int j = i + 1; j < amount.length - 1; j++) {
                for (int k = j + 1; k < amount.length; k++) {
                    int temp = amount[i] + amount[j] + amount[k];
                    if (money >= temp) {
                        max = Math.max(temp, max);
                    }
                }
            }
        }
        if (max == 0) {
            System.out.println(-1);
        } else {
            System.out.println(max);
        }

    }


}
